<?php

namespace Admin\Controller;

use Think\Controller;

class BusinessController extends Controller {

    public function __construct() {
        parent::__construct();
        $l = D('Login', 'Logic');
        $re = $l->islogin();
        if (!$re) {
            redirect('/admin/login');
        }
        $this->business_add = new \Home\Logic\Business_addLogic();
    }

    /* 申请入驻列表 */
    public function index() {
        $page = (int) $_GET['p'];
        $list = $this->business_add->getBusinessByPage($page);
        $this->assign('page',$list['page']);
        $this->assign('list',$list['list']);
        $this->display('index');
    }
    
    /*审核通过*/
    public function dispose(){
        $id = (int)$_GET['id'];
        $mobile = trim($_GET['mobile']);
        $status = 1;
        $u = new \Home\Logic\PassportLogic();
        $check = $u->adminCheck($mobile);
        if($check){
            exit('check');
        }
        if($id>0){
            $re = $this->business_add->dispose($id,$status);
            if($re){
               $business = $this->business_add->getBusinessById($id);
               if($business){
                    //插入user表
                    $u = new \Home\Logic\PassportLogic();
                    //$password = array_rand(range(0, 9), 6);
                    //$password = implode('',$password);
                    $password = '';
                    for($i=0;$i<6;$i++){
                        $password .= rand(0,9);
                    }
                    $data['user_name'] = $business['name'];
                    $data['account'] = $business['mobile'];
                    $data['password'] = $password;
                    $data['create_time'] = time();
                    $data['status'] = 1;
                    $data['is_admin'] = 1;
                    $user = $u->insertUser($data);
                    if($user){
                        //发送短信
                        import('MsgCode','Msgcode');
                        $m = new \Msgcode\MsgCode(true);
                        //$password = implode('',$password);
                        $_contents = '6&1&KAKA平台'.'&'.$business['store_name'].'&'.$business['mobile'].'&'.$password;
                        $res = $m->sendMsg_b($business['mobile'],$_contents,true);
                        if($res){
                            exit('true');
                        }
                    }else{
                        exit('false');
                    }
                } 
            }
        }
        exit;
    }

     /*审核未通过*/
     public function disposess(){
        $id = (int)$_GET['id'];
        $status = 2;
        if($id>0){
            $re = $this->business_add->dispose($id,$status);
            if($re){
                exit('true');
            }else{
                exit('false');
            }
        }else{
            exit('false');
        }
        exit;
     }






}
